package com.ruoyi.ucar.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.ucar.boreq.CarouselImageDetailQueryBoReq;
import com.ruoyi.ucar.boreq.CarouselImageListQueryBoReq;
import com.ruoyi.ucar.boresp.CarouselImageDetailQueryBoResp;
import com.ruoyi.ucar.boresp.CarouselImageListQueryBoResp;
import com.ruoyi.ucar.boresp.CarouselImageMobileListBoResp;
import com.ruoyi.ucar.common.CommonVoResp;
import com.ruoyi.ucar.domain.TCarouselImage;
import com.ruoyi.ucar.service.ITCarouselImageService;
import com.ruoyi.ucar.util.CommonUtil;
import com.ruoyi.ucar.util.ResponseUtil;
import com.ruoyi.ucar.voreq.*;
import com.ruoyi.ucar.voresp.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 轮播图主Controller
 * 
 * @author sungangbin
 * @date 2025-05-13
 */
@Api(value = "轮播图管理",tags = "轮播图管理模块接口")
@RestController
@RequestMapping("/ucar/carouselImage")
public class TCarouselImageController extends BaseController
{



    @Autowired
    private ITCarouselImageService tCarouselImageService;


    @ApiOperation("轮播图列表查询-mobile使用-匿名")
    @PostMapping("/mobileList")
    @Anonymous
    public  CommonVoResp<CarouselImageMobileListVoResp>  mobileList( )
    {

        CommonVoResp<CarouselImageMobileListVoResp> commonVoResp=new CommonVoResp<>();
        CarouselImageMobileListVoResp  carouselImageMobileListVoResp=new CarouselImageMobileListVoResp();
        List<CarouselImageMobileListVo> carouselImageMobileListVos=new ArrayList<>();
        CarouselImageMobileListBoResp carouselImageMobileListBoResp=tCarouselImageService.mobileList();
        carouselImageMobileListBoResp.getCarouselImageMobileListBos().forEach(carouselImageMobileListBo -> {
            CarouselImageMobileListVo carouselImageMobileListVo=CommonUtil.objToObj(carouselImageMobileListBo,CarouselImageMobileListVo.class);
            carouselImageMobileListVos.add(carouselImageMobileListVo);
        });
        carouselImageMobileListVoResp.setCarouselImageMobileListVos(carouselImageMobileListVos);
        commonVoResp.setData(carouselImageMobileListVoResp);
        logger.info("轮播图移动端查询VO端VO返回结果：{}",carouselImageMobileListVoResp);
        return commonVoResp;
    }



    /**
     * 查询轮播图主列表
     */
    @ApiOperation("轮播图列表查询")
    @PreAuthorize("@ss.hasPermi('ucar:image:list')")
    @PostMapping("/list")
    public  CommonVoResp<CarouselImageListQueryVoResp>  list(@RequestBody CarouselImageListQueryVoReq carouselImageListQueryVoReq)
    {
        logger.info("轮播图列表查询VO端入口参数:{}",carouselImageListQueryVoReq);
        CommonVoResp<CarouselImageListQueryVoResp> commonVoResp=new CommonVoResp<>();
        CarouselImageListQueryVoResp carouselImageListQueryVoResp=new CarouselImageListQueryVoResp();
        List<CarouselImageListQueryVo> carouselImageListQueryVos=new ArrayList<>();
        if(
                carouselImageListQueryVoReq.getPageIndex()==null||
                carouselImageListQueryVoReq.getPageSize()==null){
            return CommonUtil.setCommonVoRespNoParam(commonVoResp);
        }
        CarouselImageListQueryBoReq carouselImageListQueryBoReq=CommonUtil.objToObj(carouselImageListQueryVoReq, CarouselImageListQueryBoReq.class);
        carouselImageListQueryBoReq.setPageIndex(carouselImageListQueryVoReq.getPageIndex());
        carouselImageListQueryBoReq.setPageSize(carouselImageListQueryVoReq.getPageSize());
        logger.info("轮播图列表查询VO端VO===>BO结果：{}",carouselImageListQueryBoReq);
        CarouselImageListQueryBoResp carouselImageListQueryBoResp=tCarouselImageService.queryCarouselImageByCond(carouselImageListQueryBoReq);
        logger.info("轮播图列表查询VO端BO响应结果：{}",carouselImageListQueryBoResp);
        carouselImageListQueryVoResp.setTotalCount(carouselImageListQueryBoResp.getTotalCount());
        carouselImageListQueryVoResp.setTotalPage(carouselImageListQueryBoResp.getTotalPage());
        carouselImageListQueryBoResp.getCarouselImageListQueryBos().forEach(carouselImageListQueryBo-> {
            CarouselImageListQueryVo carouselImageListQueryVo=  CommonUtil.objToObj(carouselImageListQueryBo, CarouselImageListQueryVo.class);
            carouselImageListQueryVos.add(carouselImageListQueryVo);
        });
        carouselImageListQueryVoResp.setCarouselImageListQueryVos(carouselImageListQueryVos);
        commonVoResp.setData(carouselImageListQueryVoResp);
        logger.info("轮播图列表查询VO端VO返回结果：{}",carouselImageListQueryVoResp);
        return commonVoResp;
    }

    /**
     * 导出轮播图主列表
     */
    @PreAuthorize("@ss.hasPermi('ucar:image:export')")
    @Log(title = "轮播图主", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TCarouselImage tCarouselImage)
    {
        List<TCarouselImage> list = tCarouselImageService.selectTCarouselImageList(tCarouselImage);
        ExcelUtil<TCarouselImage> util = new ExcelUtil<TCarouselImage>(TCarouselImage.class);
        util.exportExcel(response, list, "轮播图主数据");
    }

    /**
     * 获取轮播图主详细信息
     */
    @ApiOperation("获取轮播图详细信息接口-id和carouselImageUuid，两者传一即可")
    @PreAuthorize("@ss.hasPermi('ucar:image:query')")
    @PostMapping(value = "/detail")
    public CommonVoResp<CarouselImageDetailQueryVoResp> carouselImageDetailQuery(@RequestBody CarouselImageDetailQueryVoReq carouselImageDetailQueryVoReq )
    {
        CommonVoResp<CarouselImageDetailQueryVoResp> commonVoResp=new CommonVoResp<>();
        logger.info("获取轮播图详细信息入参：{}",carouselImageDetailQueryVoReq);
        if(
                carouselImageDetailQueryVoReq.getCarouselImageUuid()==null&&
                        carouselImageDetailQueryVoReq.getId()==null){
            return CommonUtil.setCommonVoRespNoParam(commonVoResp);
        }
        CarouselImageDetailQueryBoResp carouselImageDetailQueryBoResp=tCarouselImageService.carouselImageDetailQuery(CommonUtil.objToObj(carouselImageDetailQueryVoReq, CarouselImageDetailQueryBoReq.class));

        if(StringUtils.isNull(carouselImageDetailQueryBoResp)){
            return CommonUtil.setCommonVoRespNoDataError(commonVoResp);
        }else{
            CarouselImageDetailQueryVoResp carouselImageDetailQueryVoResp=CommonUtil.objToObj(carouselImageDetailQueryBoResp,CarouselImageDetailQueryVoResp.class);
            commonVoResp.setData(carouselImageDetailQueryVoResp);
        }
        logger.info("获取轮播图详细信息出参：{}",commonVoResp);
        return commonVoResp;
    }

    /**
     * 新增轮播图主
     */
    @ApiOperation("新增轮播图接口")
    @PreAuthorize("@ss.hasPermi('ucar:image:add')")
    @PostMapping("/add")
    public CommonVoResp<CarouselImageVoResp> add(CarouselImageAddVoReq carouselImageVoReq)
    {
        CommonVoResp<CarouselImageVoResp> commonVoResp=new CommonVoResp<>();
        logger.info("新增轮播图入参：{}",carouselImageVoReq);
        try {
            String carouselImageUuid=tCarouselImageService.saveCarouselImage(carouselImageVoReq);
            CarouselImageVoResp carouselImageVoRes=new CarouselImageVoResp();
            carouselImageVoRes.setCarouselImageUuid(carouselImageUuid);
            commonVoResp.setData(carouselImageVoRes);
            logger.info("新增轮播图出参：{}",commonVoResp);
            return commonVoResp;
        } catch (IOException e) {
            commonVoResp = ResponseUtil.setCommonVoRespErrorFile(commonVoResp);
            return commonVoResp;
        }
    }

    /**
     * 修改轮播图主
     */
    @ApiOperation("修改轮播图接口")
    @PreAuthorize("@ss.hasPermi('ucar:image:edit')")
    @PostMapping("/update")
    public CommonVoResp<String> edit(CarouselImageUpdateVoReq updateCarouselImageVoReq)
    {
        CommonVoResp<String> commonVoResp=new CommonVoResp<>();
        logger.info("修改轮播图入参：{}",updateCarouselImageVoReq);
        try {
            return ResponseUtil.commonUpdateResult(tCarouselImageService.updateCarouselImage(updateCarouselImageVoReq),commonVoResp);
        } catch (IOException e) {
            return ResponseUtil.commonUpdateResult(0,commonVoResp);
        }
    }

    /**
     * 删除轮播图
     */
    @ApiOperation("删除轮播图接口-id和carouselImageUuid，两者传一即可")
    @PreAuthorize("@ss.hasPermi('ucar:image:remove')")
    @PostMapping("/delete")
    public CommonVoResp<String>  delete(@RequestBody DeleteCarouselImageVoReq deleteCarouselImageVoReq)
    {
        CommonVoResp<String> commonVoResp=new CommonVoResp<>();
        if(
                deleteCarouselImageVoReq.getCarouselImageUuid()==null&&
                        deleteCarouselImageVoReq.getId()==null){
            return CommonUtil.setCommonVoRespNoParam(commonVoResp);
        }
        logger.info("删除轮播图入参：{}",deleteCarouselImageVoReq);
        return ResponseUtil.commonDeleteResult(tCarouselImageService.deleteTCarouselImageById(deleteCarouselImageVoReq.getCarouselImageUuid(),deleteCarouselImageVoReq.getId()),commonVoResp);
    }

    /**
     * 修改轮播图状态
     */
    @ApiOperation("修改轮播图状态接口")
    @PreAuthorize("@ss.hasPermi('ucar:image:edit')")
    @PostMapping("/updateStatus")
    public CommonVoResp<String>  updateStatus(@RequestBody UpdateStatusCarouselImageVoReq updateStatusCarouselImageVoReq)
    {
        CommonVoResp<String> commonVoResp=new CommonVoResp<>();
        logger.info("修改轮播图状态入参：{}",updateStatusCarouselImageVoReq);
        return ResponseUtil.commonUpdateResult(tCarouselImageService.updateStatus(updateStatusCarouselImageVoReq.getCarouselImageUuid(),updateStatusCarouselImageVoReq.getStatus()),commonVoResp);
    }


    /**
     * 批量删除轮播图
     */
    @ApiOperation("批量删除轮播图接口-ids和carouselImageUuids，两者传一即可")
    @PreAuthorize("@ss.hasPermi('ucar:image:remove')")
    @PostMapping("/batchDelete")
    public CommonVoResp<String>  batchDelete(@RequestBody BatchDeleteCarouselImageVoReq batchDeleteCarouselImageVoReq)
    {
        CommonVoResp<String> commonVoResp=new CommonVoResp<>();
        if((StringUtils.isNull(batchDeleteCarouselImageVoReq.getCarouselImageUuids())||batchDeleteCarouselImageVoReq.getCarouselImageUuids().length==0)
               &&(StringUtils.isNull(batchDeleteCarouselImageVoReq.getIds())||batchDeleteCarouselImageVoReq.getIds().length==0)){
            return CommonUtil.setCommonVoRespNoParam(commonVoResp);
        }
        logger.info("批量删除轮播图入参：{}",batchDeleteCarouselImageVoReq);
        return ResponseUtil.commonDeleteResult(tCarouselImageService.batchDelete(batchDeleteCarouselImageVoReq.getCarouselImageUuids(),batchDeleteCarouselImageVoReq.getIds()),commonVoResp);
    }

}
